<?
/*
	Выбирает данные используя одну кодировку и записывает их в другой стобец используя другую кодировку
*/
require_once ('config.php');

$mysqli_link = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if (mysqli_connect_errno()) {
    printf("Не удалось подключиться: %s\n", mysqli_connect_error());
    exit();
}

$mysqli_link->query("SET NAMES 'latin1'");
if (($mysqli_link->affected_rows) === -1) {
	echo "Error at line " . __LINE__ . PHP_EOL . " Query: " . $sql_query . PHP_EOL;
}

//$sql_query = "SELECT id, privateid FROM " . DB_TABLE_CATALOG . " WHERE `has_descr` = 0 group by privateid limit 100";
$sql_query = "select * from `" . DB_TABLE_INFO . "` where converted = 0 limit 5000";
$result = $mysqli_link->query($sql_query);
if (mysqli_num_rows ($result) == 0) {
	echo 'Nothing to do' . PHP_EOL;
}
if (($mysqli_link->affected_rows) === -1) {
	echo "Error at line " . __LINE__ . PHP_EOL . " Query: " . $sql_query . PHP_EOL . 'Error: ' . $mysqli_link->error . PHP_EOL;
}
while ($sql_row = $result->fetch_assoc()) {
	$sql_query = 'UPDATE `' . DB_TABLE_INFO . '` SET `converted` = \'1\', description2="' . $mysqli_link->real_escape_string ($sql_row['description']) . '" WHERE `' . DB_TABLE_INFO . '`.`id` =' . $sql_row['id'];
	$mysqli_link->query("SET NAMES 'utf8'");
	$result2 = $mysqli_link->query($sql_query);
	if (($mysqli_link->affected_rows) === -1) {
		echo "Error at line " . __LINE__ . PHP_EOL . " Query: " . $sql_query . PHP_EOL . 'Error: ' . $mysqli_link->error . PHP_EOL;
	}
	$mysqli_link->query("SET NAMES 'latin1'");	
}

?>